<script setup>
// import {ref} from "vue";
import {reactive, ref} from 'vue'
import { ElMessage } from 'element-plus'
import axios from 'axios'
import { useRouter } from 'vue-router'

const isRegister = ref(false)



const loginData = ref({
  username: '',
  password: '',
})


const registerData = ref({
  username: '',
  password: '',
  rePassword: '',
})

// 密码校验
const passwordValidator = (rule, value, callback) => {
  if (value == '') {
    callback(new Error('请再次输入密码'))
    // 响应式数据要先.value
  } else if (registerData.value.password != value) {
    callback(new Error(`两次输入密码不同`))
  } else {
    callback()
  }
  return
}


const router = useRouter()

const register = async () => {
  registerData.value.username = registerData.value.username.trim()
  registerData.value.password = registerData.value.password.trim()
  registerData.value.rePassword = registerData.value.rePassword.trim()
  // 发送注册请求
  axios.post("http://localhost:8090/register/"+registerData.value.username+"/"+registerData.value.password+"").then((response) => {
    console.log(response)

    if (response.data == "success") {

      ElMessage.success("注册成功")
      // router.push("/profile")
      // _this.route.path('/main')
    }
    else {
      ElMessage.error("注册失败:用户名重复或者其他原因！！")
    }
  })

  // ElMessage.success("注册成功")
}


const onSubmit = async () => {
  try {

    // 假设这是你的登录API端点
    axios.get("http://localhost:8090/logintest2/"+loginData.value.username+"/"+loginData.value.password+"").then((response) => {
      console.log(response)

      if (response.data === "success") {

        ElMessage.success("登录成功")
        router.push("/profile")
        // _this.route.path('/main')
      }
      else {
        ElMessage.error("登录失败")
      }
    })


// const isRegister =async() =>{
//       console.log("ok")
//     }



  } catch (error) {
    // 请求出错，显示错误信息
    ElMessage.error("请求错误")
  }
}

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 16, message: '长度为3-16位', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 16, message: '长度为6-16位', trigger: 'blur' },
  ],
  rePassword: [
    {
      required: true,
      validator: passwordValidator,
      message: '两次输入密码不同',
      trigger: 'blur',
    },
  ],
}



</script>
<template>
  <div class="login-container">
    <el-row class="login-page" type="flex" justify="center">
      <!-- 左侧装饰列 -->
      <el-col :span="11" class="bg">
        <div class="bg-content">
          <h2 style="color: white; text-align: center; margin-top: 50px;">欢迎来到文章管理系统</h2>
          <p style="color: white; text-align: center; margin-top: 20px;">便捷管理资源</p>
        </div>
      </el-col>
      <!-- 间隔列 -->
      <el-col :span="2" class="spacing-column"></el-col>
      <!-- 右侧表单列 -->
      <el-col :span="11" class="form">
        <!-- 这里放置登录和注册表单切换相关内容 -->
        <el-form
            :rules="rules"
            :model="registerData"
            ref="form"
            size="large"
            autocomplete="off"
            v-if="isRegister"
        >
          <el-form-item>
            <h1 style="text-align: center; margin-bottom: 30px; color: #303133;">注册</h1>
          </el-form-item>

          <el-form-item prop="username">
            <el-input
                v-model="registerData.username"
                :prefix-icon="User"
                placeholder="请输入用户名"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
                v-model="registerData.password"
                :prefix-icon="Lock"
                type="password"
                placeholder="请输入密码"
            ></el-input>
          </el-form-item>
          <el-form-item prop="rePassword">
            <el-input
                v-model="registerData.rePassword"
                :prefix-icon="Lock"
                type="password"
                placeholder="请再次输入密码"
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="register" style="width: 100%; height: 40px; margin-top: 30px;">注册</el-button>
          </el-form-item>

          <el-form-item class="flex">
            <el-link type="info" :underline="false" @click="isRegister = false">
              ← 返回
            </el-link>
          </el-form-item>
        </el-form>

        <!-- 登录表单 -->
        <el-form
            :rules="rules"
            :model="loginData"
            ref="form"
            size="large"
            autocomplete="off"
            v-else
        >
          <el-form-item>
            <h1 style="text-align: center; margin-bottom: 30px; color: #303133;">登录</h1>
          </el-form-item>
          <el-form-item prop="username">
            <el-input
                v-model="loginData.username"
                :prefix-icon="User"
                placeholder="请输入用户名"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
                v-model="loginData.password"
                :prefix-icon="Lock"
                type="password"
                placeholder="请输入密码"
            ></el-input>
          </el-form-item>
          <!-- 登录按钮 -->
          <el-form-item>
            <el-button
                type="primary"
                @click="onSubmit"
                auto-insert-space
                style="width: 100%; height: 40px; margin-top: 30px;"
            >登录</el-button>
          </el-form-item>
          <el-link
              type="info"
              :underline="false"
              @click="isRegister = true"
              class="register-link"
          >
            注册 →
          </el-link>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>



/* 你可以在这里添加全局样式，或者直接在组件样式中覆盖 */
.el-button {
<style>
.login-container {
  background-image: url('/src/assets/img_1.png');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
}

  .login-page {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    max-width: 1200px; /* 限制最大宽度 */
  }

  .bg, .form {
    background-clip: padding-box;
    border-radius: 10px;
    padding: 20px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    margin: 0;
    height: 100%;
    min-height: 500px; /* 设置最小高度 */
  }

  .bg {
    background-image: url('/src/assets/img.png');
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    border-radius: 0 50px 50px 0;
    width: 48%; /* 减去间隔的宽度 */
  }

  .form {
    background: rgba(255, 255, 255, 0.8);
    backdrop-filter: blur(10px);
    width: 48%; /* 减去间隔的宽度 */
  }

  .spacing-column {
    width: 4%; /* 设置间隔宽度 */
  }

  .el-form-item {
    margin-bottom: 24px;
  }

  .el-button {
    height: 56px;
    font-size: 18px;
    letter-spacing: 1px;
    border-radius: 28px;
    padding: 0 40px;
  }

  .el-input__inner {
    height: 56px;
    padding: 0 20px;
    font-size: 16px;
    border-radius: 28px;
  }

  .register-link {
    color: #409EFF;
    font-size: 14px;
    font-weight: 500;
    margin-left: 10px;
    transition: color 0.3s ease;
  }
</style>

